package service

import (
	"errors"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service/dto"

	"tuowei.com/common/actions"
	cDto "tuowei.com/common/dto"
	"tuowei.com/pkg/xlog"
)

type SysExamination struct {
	service.Service
}

// GetPage 获取SysExamination列表
func (e *SysExamination) GetPage(c *dto.ApiExaminationGetPageReq, p *actions.DataPermission, list *[]models.SysExamination, count *int64) error {
	var err error
	var data models.SysExamination

	err = e.Orm.Model(data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
		).
		Where("status = 1").
		Order("id desc").
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return err
	}
	return nil
}

// Get 获取SysCourse对象
func (e *SysExamination) Get(d *dto.SysExaminationById, p *actions.DataPermission, model *models.SysExamination) error {
	var data models.SysExamination

	err := e.Orm.Model(&data).Debug().
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error

	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		xlog.Errorf("db error: %s", err)
		return err
	}

	return nil
}
